Howtodoit...
1. Firstandforemost,justaswithiOSnativemodules,you'llwanttolimitthe
amountofdatacrossingtheReactNativebridge.Keepingthedatathat'sin
eventsandcallbackstoaminimumwillhelptoavoidslowdownscausedby
theserializationbetweenJavaandJavaScript.Also,aswithiOS,trytokeep
datacachedinmemorytobeusedbythenativemodule;keepitstoredina
privatefield.Nativemodulesaresingletons.Thisshouldbeleveraged
insteadofreturningalargeobjecttostoreintheReactNativecomponent.
2. WhenwritingJavacodeforAndroid,youshoulddoyourbesttoavoid
creatingshort-termobjects.Ifyoucan,useprimitives,especiallyfor
datasetssuchasarrays.
3. Itisbettertoreuseobjectsinsteadofrelyingonthegarbagecollectorto
pickupanunusedreferenceandinstantiateanewobject.
4. TheAndroidSDKprovidesamemory-efficientdatastructureforreplacing
theuseofaMap,whichmapsintegerstoobjects,calledSparseArray.Usingit
canreducememoryusageandimproveperformance.Here'sanexample:
SparseArray<SomeType>map=newSparseArray<SomeType>();
map.put(1,myObjectInstance);
ThereisalsoSparseIntArray,whichmapsintegerstointegers,andSparseBooleanArray,whichmaps
integerstoBooleanvalues.
5. WhileitmaysoundcounterintuitivetodevelopersusedtoOOP
developmentinJava,avoidingtheuseofgettersandsettersbyaccessing
theinstancefielddirectlycanalsoimproveperformance.
6. Ifyou'reeverworkingwithStringconcatenation,makeuseofStringBuilder.
7. Lastly,themostsignificantperformanceoptimizationyoucanmake,if
possible,isspawningasynchronousbackgroundthreadstoperformheavy
computationsbyleveragingReactNative'sasynchronousmessaging/event
systemtocommunicatebetweentheJavaScriptandnativethreads.When
yourbackgroundprocessiscomplete,youcaneitherinvokea
callback/PromiseorfireaneventfortheJavaScriptthreadtopickup.To
learnhowtocreatebackgroundprocessesinReactNativeAndroidnative
modules,pleasereadtheBackgroundprocessingonAndroidrecipeinChapte
r11,AddingNativeFunctionality.